<template>
<div id="text-reader-mask">
  <div id="text-reader-box">
    <div>文本编辑</div>
    <el-form id="form-box">
      <el-form-item>
        <el-upload action="/" :auto-upload="false" :on-change="openFile">
          <el-button type="primary">打开文件</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-input type="textarea" :rows="10" v-model="text"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="$emit('close')">关闭</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
export default {
  name: 'TextReader',
  methods: {
    openFile (file) {
      let reader = new FileReader()
      reader.readAsText(file.raw)
      reader.onload = () => {
        this.text = reader.result
      }
    },

    save () {
      this.$emit('save', this.text)
    }
  },
  data () {
    return {
      text: ''
    }
  }
}
</script>

<style scoped>
#form-box {
  width: 600px;
}

#text-reader-mask {
  position: absolute;
  left: 0;
  top: 0;
  background-color: rgba(0,0,0, 0.3);
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
}

#text-reader-box {
  background-color: #ffffff;
  padding: 20px;
  border-radius: 5px;
}

#textarea {
  height: 400px;
  width: 200px;
}
</style>